package com.hyperzsoft.budgeteer;

import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.hyperzsoft.budget.BudgetDBProvider.BudgetTable;
import com.hyperzsoft.budget.BudgetDBProvider.Credit;
import com.hyperzsoft.budget.BudgetDBProvider.Expense;
import com.hyperzsoft.budget.BudgetDBProvider.Monthly;

public class CreditGrid extends Activity {
	
	static private String TAG = "CreditGrid";
	private BudgetDBProvider dbProvider;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.credit_grid);
		
		dbProvider = new BudgetDBProvider(this.getApplicationContext());
		doTestRun();
		GridView gridview = (GridView) findViewById(R.id.BudgetList);
	    gridview.setAdapter(new budgetGridAdapter(getApplicationContext()));
	    dbProvider.close();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	    dbProvider.close();
	}
	
	public void doTestRun() {
		dbProvider.deleteAll(BudgetTable.CREDIT);
		dbProvider.deleteAll(BudgetTable.EXPENSE);
		dbProvider.deleteAll(BudgetTable.MONTHLY);
		
		long newCreditId = dbProvider.insert(BuildCreditItem());
		Log.d(TAG, "New Credit Row ID: " + newCreditId);
		
		long newExpId = dbProvider.insert(BuildExpenseItem());
		Log.d(TAG, "New Expense Row ID: " + newExpId);
		
		long newRowId = dbProvider.insert(BuildMonthlyItem(newCreditId, newExpId));
		Log.d(TAG, "New Monthly Row ID: " + newRowId);
	}
	
	public Monthly BuildMonthlyItem(long creditID, long expenseID) {
		Monthly monthly = new Monthly();
		monthly.CREDIT_ID = creditID;
		monthly.EXPENSE_ID = expenseID;
		monthly.DUE_DATE = new Date("Feb 01, 2011").getTime();
		monthly.AMOUNT_PAID = 775.00;
		monthly.BALANCE = 0;
		return monthly;
	}
	
	public Expense BuildExpenseItem() {
		Expense expense = new Expense();
		expense.NAME = "Rent";
		expense.TYPE = "Monthly Expense";
		expense.AMOUNT = 775.00;
		expense.AMOUNT_TYPE = "Regular/Monthly";
		return expense;
	}
	
	public Credit BuildCreditItem() {
		Credit credit = new Credit();
		credit.AMOUNT = 1761;
		credit.CREDIT_DATE = new Date("Feb 04, 2011").getTime();
		return credit;
	}
	
	public class budgetGridAdapter extends BaseAdapter {
		private ArrayList<Object> budget;
		private ArrayList<Credit> credit_list;
		private BudgetDBProvider db_table;
		
		public budgetGridAdapter(Context context) {
			db_table = new BudgetDBProvider(context);
			budget = db_table.selectAll(BudgetTable.CREDIT);
			db_table.close();
			
			//Argggh.. no way to cast the array list into a specific ArrayList of budget objects??
			credit_list = new ArrayList<Credit>();
			for (Object obj : budget){
				credit_list.add((Credit)obj);
			}
		}
		
		public int getCount() {
		    return credit_list.size();
		}
		
		public Object getItem(int position) {
		    return null;
		}
		
		public long getItemId(int position) {
		    return 0;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
		    View itemView;
		    
		    if (convertView == null) {
		    	LayoutInflater li = getLayoutInflater();
		    	itemView = li.inflate(R.layout.grid_item, null);
		    	
		    	TextView id = (TextView)itemView.findViewById(R.id.item_id);
		    	id.setText((credit_list.get(position)._ID + ": "));
		    	
		    	TextView amount = (TextView)itemView.findViewById(R.id.item_amount);
		    	amount.setText("$" + (credit_list.get(position).AMOUNT));
		    	
		    	Date credit_date = new Date();
		    	credit_date.setTime(credit_list.get(position).CREDIT_DATE);
		    	TextView created = (TextView)itemView.findViewById(R.id.item_created_date);
		    	created.setText(credit_date.toLocaleString());
		    }
		    else {
		    	itemView = (View) convertView;
		    }
		    
		    return itemView;
		}
	}
}